<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

class BakerysModelBakerys extends JModelList
{
	protected function populateState($ordering = null, $direction = null)
	{
		$params = JComponentHelper::getParams("com_bakerys");
		$limit = $params->get("pagelimit",2);
		$this->setState('list.limit', $limit);
		$limitstart = JRequest::getUInt('limitstart', 0);
		$this->setState('list.start', $limitstart);
	}

	function getListQuery()
	{
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);
		$input = JFactory::getApplication()->input;
		$catid = $input->get("catid");
		$location_id = $input->get("location");
		
		$query->select("i.*,li.price as location_price")
			->from("#__bakery_items i")
			->leftJoin("#__bakery_location_items li on li.item_id = i.id and location_id = $location_id")
			->where("i.state = 1");
		if(!empty($catid))
		{
			$query->select("c.title as category_title");
			$query->leftJoin("#__categories c on c.id = i.catid");
			$query->where("i.catid = $catid");
		}
		return $query;
	}
	
}
